<?php
/**
 * @package  member
 */

class MemberGridFieldDetailForm extends GridFieldDetailForm {
}

class MemberGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest {
    private static $allowed_actions = array(
        'edit',
        'view',
        'ItemEditForm',
        'doResendWelcomeEmail'
    );
	
	public function ItemEditForm() {
		$list = $this->gridField->getList();

		if (empty($this->record)) {
			$controller = $this->getToplevelController();
			$noActionURL = $controller->removeAction($_REQUEST['url']);
			$controller->getResponse()->removeHeader('Location');   //clear the existing redirect
			return $controller->redirect($noActionURL, 302);
		}

		$canView = $this->record->canView();
		$canEdit = $this->record->canEdit();
		$canDelete = $this->record->canDelete();
		$canCreate = $this->record->canCreate();

		if(!$canView) {
			$controller = $this->getToplevelController();
			// TODO More friendly error
			return $controller->httpError(403);
		}

		$actions = new FieldList();
		if($this->record->ID !== 0) {
			if($canEdit) {
				$actions->push(FormAction::create('doSave', _t('MemberGridFieldDetailForm.Save', 'Save'))
					->setUseButtonTag(true)
					->addExtraClass('ss-ui-action-constructive')
					->setAttribute('data-icon', 'accept'));
			}

			if($canDelete) {
				$actions->push(FormAction::create('doDelete', _t('MemberGridFieldDetailForm.Delete', 'Delete'))
					->setUseButtonTag(true)
					->addExtraClass('ss-ui-action-destructive action-delete'));
			}

		}else{ // adding new record
			//Change the Save label to 'Create'
			$actions->push(FormAction::create('doSave', _t('MemberGridFieldDetailForm.Create', 'Create'))
				->setUseButtonTag(true)
				->addExtraClass('ss-ui-action-constructive')
				->setAttribute('data-icon', 'add'));
				
			// Add a Cancel link which is a button-like link and link back to one level up.
			$curmbs = $this->Breadcrumbs();
			if($curmbs && $curmbs->count()>=2){
				$one_level_up = $curmbs->offsetGet($curmbs->count()-2);
				$text = sprintf(
					"<a class=\"%s\" href=\"%s\">%s</a>",
					"crumb ss-ui-button ss-ui-action-destructive cms-panel-link ui-corner-all", // CSS classes
					$one_level_up->Link, // url
					_t('MemberGridFieldDetailForm.CancelBtn', 'Cancel') // label
				);
				$actions->push(new LiteralField('cancelbutton', $text));
			}
		}

		if($this->record->ID !== 0) {
            $actions->push(FormAction::create('doResendWelcomeEmail', _t('MemberGridFieldDetailForm.RESEND_WELCOME_EMAIL', 'Resend Welcome Email'))->setAttribute('data-icon', 'accept')->setUseButtonTag(true));
        }

		$fields = $this->component->getFields();
		if(!$fields) $fields = $this->record->getCMSFields();
		
		$fields->addFieldToTab('Root.Main',
			ListboxField::create('DirectGroups', singleton('Group')->i18n_plural_name())->setMultiple(true)->setSource(Group::get()->filter('IsDistributorGroup', 1)->map('ID', 'TitleByLang')->toArray())
		);
		
		$fields->push(HiddenField::create('IsDistributor', 'IsDistributor', 1));
		$fields->push(HiddenField::create('IsBackend', 'IsBackend', 1));
		
		if($this->record->ID !== 0){
			$fields->makeFieldReadonly('Activated');
			$fields->makeFieldReadonly('AgreeTermAndCondition');
		}
		else{
			$fields->replaceField('Activated', HiddenField::create('Activated', 'Activated', 1));
			$fields->replaceField('AgreeTermAndCondition', HiddenField::create('AgreeTermAndCondition', 'AgreeTermAndCondition', 1));
		}
		
		if($fields->dataFieldByName('DateFormat')){
			$fields->removeByName('DateFormat');
		}
		
		if($fields->dataFieldByName('TimeFormat')){
			$fields->removeByName('TimeFormat');
		}
		
		if($fields->dataFieldByName('Permissions')){
			$fields->removeByName('Permissions');
		}
		
		if($fields->dataFieldByName('HasConfiguredDashboard')){
			$fields->removeByName('HasConfiguredDashboard');
		}
		
		$sort = array(
			'MemberCode',
			'Username',
			'Password',
			'Pin',
			'FirstName',
			'Surname',
			'Email',
			'AlternateEmail',
			'Passport',
			'Mobile'
		);
		
		$fields->findOrMakeTab('Root.Main')->Fields()->changeFieldOrder($sort);
		
		if(Permission::check('CHANGE_Username')){
    		$fields->replaceField('Username', TextField::create('Username', $this->record->fieldLabel('Username')));
		}

		if(Permission::check('VIEW_Password')) {
			$fields->dataFieldByName('Password')->children->first()->setRightTitle($this->record->DecryptPassword);
		}
		
		if(Permission::check('VIEW_Pin')) {
			$fields->dataFieldByName('Pin')->children->first()->setRightTitle($this->record->DecryptPin);
		}

		// If we are creating a new record in a has-many list, then
		// pre-populate the record's foreign key. Also disable the form field as
		// it has no effect.
		if($list instanceof HasManyList) {
			$key = $list->getForeignKey();
			$id = $list->getForeignID();

			if(!$this->record->isInDB()) {
				$this->record->$key = $id;
			}

			if($field = $fields->dataFieldByName($key)) {
				$fields->makeFieldReadonly($field);
			}
		}

		// Caution: API violation. Form expects a Controller, but we are giving it a RequestHandler instead.
		// Thanks to this however, we are able to nest GridFields, and also access the initial Controller by
		// dereferencing GridFieldDetailForm_ItemRequest->getController() multiple times. See getToplevelController
		// below.
		$form = new Form(
			$this,
			'ItemEditForm',
			$fields,
			$actions,
			$this->component->getValidator()
		);
		
		$form->loadDataFrom($this->record, $this->record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT);

		if($this->record->ID && !$canEdit) {
			// Restrict editing of existing records
			$form->makeReadonly();
			// Hack to re-enable delete button if user can delete
			if ($canDelete) {
				$form->Actions()->fieldByName('action_doDelete')->setReadonly(false);
			}
		} elseif(!$this->record->ID && !$canCreate) {
			// Restrict creation of new records
			$form->makeReadonly();
		}

		// Load many_many extraData for record.
		// Fields with the correct 'ManyMany' namespace need to be added manually through getCMSFields().
		if($list instanceof ManyManyList) {
			$extraData = $list->getExtraData('', $this->record->ID);
			$form->loadDataFrom(array('ManyMany' => $extraData));
		}
		
		// TODO Coupling with CMS
		$toplevelController = $this->getToplevelController();
		if($toplevelController && $toplevelController instanceof LeftAndMain) {
			// Always show with base template (full width, no other panels), 
			// regardless of overloaded CMS controller templates.
			// TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller
			$form->setTemplate('LeftAndMain_EditForm');
			$form->addExtraClass('cms-content cms-edit-form center');
			$form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
			if($form->Fields()->hasTabset()) {
				$form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
				$form->addExtraClass('cms-tabset');
			}

			$form->Backlink = $this->getBackLink();
		}

		$cb = $this->component->getItemEditFormCallback();
		if($cb) $cb($form, $this);
		$this->extend("updateItemEditForm", $form);
		return $form;
	}

    public function doResendWelcomeEmail($data, $form) {
        $controller = $this->getToplevelController();

        try {
            $this->record->sendRegisterEmail();
        }
        catch(ValidationException $e) {
            $form->sessionMessage($e->getResult()->message(), 'bad');
            $responseNegotiator = new PjaxResponseNegotiator( array(
                'CurrentForm' => function() use (&$form) {
                    return $form->forTemplate();
                },
                'default' => function() use (&$controller) {
                    return $controller->redirectBack();
                }

            ));
            if($controller->getRequest()->isAjax()) {
                $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm');
            }
            return $responseNegotiator->respond($controller->getRequest());
        }

        $message = _t('MemberGridFieldDetailForm.SUCCESS_RESENT_WELCOME_EMAIL', 'Welcome email has been successfully resent.');
        //end custom code

        $form->sessionMessage($message, 'good');

        if($this->gridField->getList()->byId($this->record->ID)) {
            // Return new view, as we can't do a "virtual redirect" via the CMS Ajax
            // to the same URL (it assumes that its content is already current, and doesn't reload)
            return $this->edit($controller->getRequest());
        } else {
            // Changes to the record properties might've excluded the record from
            // a filtered list, so return back to the main view if it can't be found
            $noActionURL = $controller->removeAction($data['url']);
            $controller->getRequest()->addHeader('X-Pjax', 'Content');
            return $controller->redirect($noActionURL, 302);
        }
    }
}
